#include "config.h"

#include "lib/libedb/edb.h"

static void
cb_pathmon(const char *LNP, ecm_ptr_t ctx)
{
}

static void
modifier(ecm_ptr_t ctx)
{
	edb_t	*edb;
	ECMBOOL	value = ECM_FALSE;

	if (edb_connect(NULL, 0, NULL, NULL, &edb) != EDB_OK) {
		ecm_fprintf(ecm_stderr, "cannot connect\n");
		return;
	}
	ecm_fprintf(ecm_stdout, "start toggling /pathmon_test.\n");
	while (1) {
		value = value ? ECM_FALSE: ECM_TRUE;
		edb_set_bool(edb, "/pathmon_test", NULL, value);
	}
}

static void
start_modifier(void)
{
	ecm_thread_create(NULL, modifier, NULL);
}

int
main(int argc, char *argv[])
{
	edb_t	*edb;
	ecm_ptr_t	key;

	edb_initialize();

	if (edb_connect(NULL, 0, NULL, NULL, &edb) != EDB_OK) {
		ecm_fprintf(ecm_stderr, "cannot connect\n");
		return 1;
	}
	if (edb_add_pathmon(edb, "/pathmon_test", cb_pathmon, NULL, &key) != EDB_OK) {
		ecm_fprintf(ecm_stderr, "cannot add pathmon\n");
		return 1;
	}

	start_modifier();

	ecm_sleep(500);

	ecm_fprintf(ecm_stdout, "trying to del path monitor\n");
	ecm_fprintf(ecm_stdout, "Test maybe fails if it has not finished in a few seconds\n");
	if (edb_del_pathmon(edb, key) != EDB_OK) {
		ecm_fprintf(ecm_stderr, "cannot del pathmon\n");
		return 1;
	}

	ecm_fprintf(ecm_stdout, "successfully path monitor deleted\n");
	return 0;
}

